<template>
     <h2 style="text-align: center;">用户借款页面</h2>
     <table>
        <tr>
            <td>借款项目名称</td>
            <td>{{info.B_Name }}</td>
        </tr>
        <tr>
            <td>借款最高金额</td>
            <td>{{info.B_MaxMoney}}</td>
        </tr>
        <tr>
            <td>借款利息</td>
            <td>{{info.I_Interest}}</td>
        </tr>
        <tr>
            <td>借款金额</td>
            <td><input type="text" v-model="BMoney"></td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: center;">
             <input type="button" value="提交" @click="SaveData">
            </td>
        </tr>
     </table>
</template>

<script setup lang="ts">
import {ref,onMounted} from 'vue'
import axios from 'axios'
import {useRoute,useRouter} from 'vue-router'
import { useUserStore } from '../../store/index.ts';

const store = useUserStore();

const route = useRoute();

const router = useRouter();

const  info = ref({
    B_Name:"",
    B_MaxMoney:0,
    I_Interest:0
})


//生命周期函数勾
onMounted(()=>{
    LoadData()
})

//显示数据
const LoadData = ()=>{

     axios("https://localhost:44354/api/Borrows/GetBorrowInfo",{
        params:{
            Bid:route.params.Bid
        }
     }).then(res=>{
       info.value= res.data[0];
     }).catch(err=>{
        console.log(err);
     })
}

//借款金额
const BMoney = ref("")

//借款
const SaveData = ()=>{
    axios({
        url:"https://localhost:44354/api/Borrows/AddUserBorrowInfo",
        method:"POST",
        data:{
            U_Id:store.Uid,
            B_Id:route.params.Bid,
            UB_Money:BMoney.value
        }
      }).then((res)=>{
           if(res.data>0)
           {
            alert('借款成功!')
            router.push('/ShowUserBorrowInfo')
           }
           else
           {
            alert('借款失败!')
           }
      }).catch((err)=>{
        console.log(err);
      })
}
</script>